var vueApp = new Vue({
  el: '#app',
  data: function () {
    return {
    	oper:'add',  
    	title:'添加导入配置',
    	tableId:'',
    	table_name:'',
    	table_desc:'',
    	fields:[],
    	idx: 0,
    	url:'importtablelist.html',
    	uploadUrl:'../../pub/attach/uploadsingle',
    	data:{},
    	typeLimit:['application/vnd.ms-excel','application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'],
    	fileFlag: false,
    	fileUploadPercent:0,
    	tmpurl:'',
    	tmpname:'',
    	typeOptions:[]
    };
  },
  mounted: function () { 
    this.init();
  },
  methods: {
    init: function () {    
    	this.tableId = tools.getQueryString("tableId")==""?this.tableId:tools.getQueryString("tableId");
	    if(this.tableId !=""){
	    	this.oper = "edit";
	    	this.getInfo();
	    }
	    this.setTitle();    
	    this.typeOptions = data_type;
    },    
    setTitle:function(){
    	if(this.oper=="add"){
    		this.title = "添加导入配置";
    	}else if(this.oper=="edit"){
    		this.title="导入配置编辑";
    	}
    	$("title").html(this.title);
    },
    getInfo:function(){
    	var that =this;    	
        var reqData = {
           "tableId": this.tableId
        };
        tools.ajaxRequestAsyncEx("pub/import/tableinfo", reqData, function(data){
           if(data.respCode=="0"){ 
        	   console.log(data);
        	   var info = data.list[0];
        	   that.table_name = info.table_name;
        	   that.table_desc = info.table_desc;
        	   that.fields = info.detaillist.map(function(item){
        		   item.idx = that.idx++;
        		   return item;
        	   });	         	   
        	   that.tmpurl =  info.template_url;
        	   var tmps = info.template_url.split("\.");
        	   that.tmpname = info.table_name+"."+tmps[tmps.length-1];
           }else{
        	   that.showError(data.respDesc);
        	   return false;
           }     
        }); 
    },    
    beforeUploadFile: function(file) {
    	this.data.fileName= file.name;
    	this.data.fileType = file.type;
    	this.data.fileSize = file.size;
    	this.data.modeName = "导入配置模版";
		this.data.folder= "/file/tmp";
        this.data.modeName="上传";
    	
        const isLt10M = file.size / 1024 / 1024  < 50;
        if (this.typeLimit.indexOf(file.type) == -1) {
        	this.showError("上传文件格式不正确");
            return false;
        }
        if (!isLt10M) {
        	this.showError("上传文件大小不能超过50MB!");
            return false;
        }

  },
  uploadFileProcess: function(event, file, fileList){
	  var that = this;          
	  var complete = (event.loaded / event.total * 100 | 0);
      that.$showProgress(complete);
  },
  handleFileSuccess: function (res, file) {   
	  var that =this;
	  if(res.respCode=="0"){
		  var info = res.fileList[0];
	      this.tmpname = info.fileName;
	      this.tmpurl = info.filePath;
		  setTimeout(function(){
	    	  that.$showProgress(false);
	    	  that.$message({
	 	         showClose: true,
	 	         type: 'success',
	 	         message: "上传成功"
	 	      });
	      },2000);
	  }else{
		  this.showError(res.respDesc);
	  }

  },
  handleFileError:function(err, file, fileList){
	  this.$showProgress(false);
  },
    addRow:function(){
    	var row = {
    		"filed_name":"",
    		"filed_desc":"",
    		"data_type":"",
    		"real_filed_name":"",
    		"idx": this.idx++
    	};
    	this.fields.push(row);
    	this.sort();
    },
    del:function(row){
    	var that = this;
    	for(var i=0;i<this.fields.length;i++){
    		if(that.fields[i].idx == row.idx){
    			that.fields.splice(i,1);
    		}
    	} 	
    	this.sort();
    },
    sort:function(){
    	this.fields = this.fields.map(function(item,idx){
    		item.filed_name = "f"+(idx+1);
    		return item;
    	});    	
    },
    check:function(){
    	var that =this;
    	if(this.table_name.trim()==""){
    		this.showError("表名不能为空");
    		return false;
    	}
    	if(this.table_desc.trim()==""){
    		this.showError("表描述不能为空");
    		return false;
    	}
    	if(this.tmpurl.trim()==""){
    		this.showError("请上传模版");
    		return false;
    	}
    	var tmps = this.tmpurl.trim().split("\.");
    	var suffix = tmps[tmps.length-1];
    	if(suffix!="xls"&&suffix!="xlsx"){
    		this.showError("上传模版格式不正确");
    		return false;
    	}    	
    	
    	var valid = true;
    	for(var i=0;i<this.fields.length;i++){
    		var item = that.fields[i];
    		if(item.filed_name.trim()==""){
    			that.showError("字段名称不能为空");
    			valid = false;
    			break;
    		}
    		if(item.filed_desc.trim()==""){
    			that.showError("字段描述不能为空");
        		valid= false;
        		break;
    		}
    		if(item.data_type.trim()==""){
    			that.showError("数据类型不能为空");
    			valid = false;
    			break;
    		}
    		if(item.real_filed_name.trim()==""){
    			that.showError("真实字段名称不能为空");
    			valid = false;
    			break;
    		}
    	}
    	if(!valid){
    		return false;
    	}
    	
    	return true;
    },    
    save:function(){
    	var that =this;
    	if(!this.check()){
    		return false;
    	}
    	var jsonStr = {
    	   "tableId":this.tableId,
    	   "tableName": this.table_name,
    	   "tableDesc": this.table_desc,
    	   "tempUrl": this.tmpurl,
    	   "fields": this.fields
    	}; 
    	var reqData = {
           "jsonStr": JSON.stringify(jsonStr)
        };
        tools.ajaxRequestAsyncEx("pub/import/savetable", reqData, function(data){
        	if(data.respCode=="0"){
        		that.$message({
                    showClose: true,
                    type: 'success',
                    message: "保存成功"
                }); 
        		setTimeout(function(){
        			that.back();
        		}, 3000);
        	}else{
        		that.showError(data.respDesc);
        		return false;
        	}        	
        });   	
    	
    	
    },
    back:function(){
    	 location.href = this.url;
    },
    closePopup: function() {
        this.$refs["btn"].click();
    },
   
    showError: function(info){
        var i = (info==undefined?"加载数据错误":info);
        this.$alert(i, '系统错误', {
             confirmButtonText: '确定'
        }); 
    },   


  },
  watch: {
	
  }
  
});